﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using ShevaEngine.Core.Modules.Materials;

namespace ShevaEngine.Core.Modules.Models
{
    /// <summary>
    /// Material description.
    /// </summary>
    public class ModelMaterialParameters
    {
        /// <summary>Material name.</summary>
        public string Name { get; set; }
        /// <summary>Diffuse color.</summary>
        public Color Diffuse { get; set; }
        /// <summary>Specular color.</summary>
        public Color Specular { get; set; }
        /// <summary>Emissive color.</summary>
        public Color Emissive { get; set; }
        /// <summary>Specular power.</summary>
        public float SpecularPower { get; set; }
        /// <summary>Used textures.</summary>              
        public Dictionary<string, string> Textures;
        /// <summary>Alpha.</summary>
        public float Alpha
        {
            //get { return Math.Min(1.0f, Math.Max(0.0f, ((float)this.Diffuse.A / (float)byte.MaxValue))); }
            //set { this.Diffuse.A = (byte)(value * (float)byte.MaxValue); }
            get;
            set;
        }

        /// <summary>
        /// Konstruktor.
        /// </summary>
        public ModelMaterialParameters()
        {
            this.Diffuse = Color.White;
            this.Textures = new Dictionary<string, string>();
        }
    }

    /// <summary>
    /// Equality comparer.
    /// </summary>
    public class ModelMaterialParametersEqualityComparer : IEqualityComparer<ModelMaterialParameters>
    {
        /// <summary>
        /// Equals method.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool Equals(ModelMaterialParameters x, ModelMaterialParameters y)
        {
            if (x.Name != y.Name ||
                x.Diffuse != x.Diffuse ||
                x.Specular != x.Specular ||
                x.Emissive != x.Emissive ||
                x.SpecularPower != x.SpecularPower ||
                x.Alpha != x.Alpha || 
                x.Textures.Count != y.Textures.Count)
                return false;

            foreach (KeyValuePair<string, string> item in x.Textures)
                if (!y.Textures.ContainsKey(item.Key) || y.Textures[item.Key] != item.Value)
                    return false;

            return true;
        }

        /// <summary>
        /// Get hash code.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int GetHashCode(ModelMaterialParameters obj)
        {
            return base.GetHashCode();
        }
    }
}
